#!/bin/bash
#
# BLABBERMOUTH: a text-obfuscation script that takes a legitimate stream of 
# text and masks it with random words sourced from the unix dictionary file,
# making it much harder for a human operator to read it.
#
# USAGE: blabbermouth [-d] [FILE]
#
# With no FILE specified, blabbermouth reads text from stdin, obfuscates it,
# and prints the obfuscated text to stdout. Otherwise, it will attempt to read
# the cleartext from the given FILE.
# 
# Pass the `-d' flag to indicate that blabbermouth should deobfuscate a 
# previously obfuscated text.
#

# this is where the file is located, at least in debian:
WORDS=/etc/dictionaries-common/words

# obfuscation logic:
encrypt() {
    # the "chaff" is sourced from the words file, but we should rotate it:
    chaff=$(shuf "$WORDS" | head -1)

    for word in $1
    do
        printf "$word $chaff "
        chaff=$(shuf "$WORDS" | head -1)
    done
    echo 
}

# deobfuscation script:
decrypt() {
    index=1
    for word in $1
    do
        rem=$(( $index % 2 ))
        if [[ ${rem} -ne 0 ]]
        then
            printf "$word "
        fi
        index=$(($index + 1))
    done
    echo
}

if [[ "$1" == "-d" ]]
then
    echo "Enter some text to be deobfuscated: "
    read token
    decrypt "$token"
else
    echo "Enter some text to be obfuscated: "
    read token
    encrypt "$token"
fi
